﻿using System;
using System.Data;
using System.Linq;
using MDSAdministration;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace MDSAdministrationTest
{
    [TestClass]
    public class ExcelUploadAdvancedTests
    {
        //Manufacturer has a duplicate
        [TestMethod]
        public void SimpleManDup()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\01_Simple_man_dup.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Manufacturer").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(true, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            var dupCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXECELUPLOAD_CODE_DUP) == true).Count();
            Assert.AreEqual(2, dupCount);
        }

        //Manufacturer has a invalid column name
        [TestMethod]
        public void SimpleManIvalidColName()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\02_Simple_man_IvalidColName.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Manufacturer").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(true, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            var dupCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXECELUPLOAD_CODE_DUP) == true).Count();
            Assert.AreEqual(0, dupCount);
            Assert.AreEqual(1, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual("InvalidCol", sheet[0].LColumnInvalidName[0]);
        }

        //Manufacturer has a invalid data type
        [TestMethod]
        public void SimpleManIntFormat()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\03_Simple_man_IntFormat.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Manufacturer").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }
            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(true, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(1, sheet[0].LColumnInvalidFormat.Count);
            Assert.AreEqual("Code", sheet[0].LColumnInvalidFormat[0]);
        }

        //Manufacturer1 is an invalid entity
        [TestMethod]
        public void SimpleEntityInvalid()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\04_Simple_Entity_Invalid.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Manufacturer1").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(true, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(0, sheet[0].LColumnInvalidFormat.Count);
        }

        //Model has a invalid Manufacturer domain attribute member
        [TestMethod]
        public void SimpleModelMissingDomain()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\05_Simple_Model_MissingDomain.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Model").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(true, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            var dupCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXECELUPLOAD_CODE_DUP) == true).Count();
            Assert.AreEqual(0, dupCount);
            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(0, sheet[0].LColumnInvalidFormat.Count);
            
            var missingCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXCELUPLOAD_MDSADMIN + "Manfacturer_Missing") == true).Count();
            Assert.AreEqual(1, missingCount);
        }

        //Model has a inactive Manufacturer domain attribute member
        [TestMethod]
        public void SimpleModelInactiveDomain()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\06_Simple_Model_InactiveDomain.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Model").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(true, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            var dupCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXECELUPLOAD_CODE_DUP) == true).Count();
            Assert.AreEqual(0, dupCount);
            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(0, sheet[0].LColumnInvalidFormat.Count);

            var missingInactive = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXCELUPLOAD_MDSADMIN + "Manfacturer_Inactive") == true).Count();
            Assert.AreEqual(1, missingInactive);
        }

        //Model has a inactive and invaid Manufacturer domain attribute members
        [TestMethod]
        public void SimpleModelMissingInactiveDomain()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\07_Simple_Model_Missing_InactiveDomain.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Model").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(true, sheet[0].IsDomainInactive);
            Assert.AreEqual(true, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            var dupCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXECELUPLOAD_CODE_DUP) == true).Count();
            Assert.AreEqual(0, dupCount);
            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(0, sheet[0].LColumnInvalidFormat.Count);
            
            var missingInactive = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXCELUPLOAD_MDSADMIN + "Manfacturer_Inactive") == true).Count();
            Assert.AreEqual(1, missingInactive);

            var missingCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXCELUPLOAD_MDSADMIN + "Manfacturer_Missing") == true).Count();
            Assert.AreEqual(1, missingCount);
        }

        //Model has a inactive and invaid Manufacturer domain attribute members
        [TestMethod]
        public void SimpleModelHasSystemAttribute()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\08_Simple_Model_Has_SystemAttribute.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Model").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], true, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(false, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(true, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            var dupCount = sheet[0].Data.AsEnumerable().Where(r => r.Field<bool>(Constants.EXECELUPLOAD_CODE_DUP) == true).Count();
            Assert.AreEqual(0, dupCount);
            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(0, sheet[0].LColumnInvalidFormat.Count);
            Assert.AreEqual(1, sheet[0].LHasSystemAttributes.Count);
            Assert.AreEqual("Status_ID", sheet[0].LHasSystemAttributes[0]);
        }

        //Manufacturer missing code
        [TestMethod]
        public void SimpleManCodeMissing()
        {
            Common.LoadServer();
            ExcelUpload euSimple = new ExcelUpload();
            euSimple.FileName = @"..\..\TestData\ExcelUploadAdvanced\09_Simple_man_CodeMissing.xlsx";
            euSimple.GetExcelSheetNames();
            var sheet = euSimple.Sheets.Where(s => s.Name == "Manufacturer").ToList();

            DataSet dsA = DataHelper.GetMdsSystemDetails(Common.ServerA);
            var modelA = new CompareModel();
            BusinessHelper.buildModelSytem(modelA, dsA, Common.ServerA);

            dsA = DataHelper.GetDataModelDetails(Common.ServerA);
            BusinessHelper.buildModel(dsA, modelA);

            var result = euSimple.LoadSpreadSheet(sheet[0], true, euSimple.FileName, modelA);
            if (result != null)
            {
                euSimple.ExcelSheetAnalysis(sheet[0], false, false, modelA, Common.ServerA);
            }

            Assert.AreEqual(true, sheet[0].IsCodeMissing);
            Assert.AreEqual(false, sheet[0].IsNameMissing);
            Assert.AreEqual(false, sheet[0].IsCodeDuplicate);
            Assert.AreEqual(false, sheet[0].IsDomainInactive);
            Assert.AreEqual(false, sheet[0].IsDomainMissing);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidName);
            Assert.AreEqual(false, sheet[0].IsColumnInvalidFormat);
            Assert.AreEqual(false, sheet[0].IsBkColumnInvalid);
            Assert.AreEqual(false, sheet[0].IsForeignKeyIvalid);
            Assert.AreEqual(false, sheet[0].IsHasSystemAttribute);
            Assert.AreEqual(false, sheet[0].IsCodeNotAutoGenerate);
            Assert.AreEqual(false, sheet[0].IsSheetNameInvalid);
            Assert.AreEqual(false, sheet[0].IsCodeNameMatchingInvalid);

            Assert.AreEqual(0, sheet[0].LColumnInvalidName.Count);
            Assert.AreEqual(0, sheet[0].LColumnInvalidFormat.Count);
            Assert.AreEqual(0, sheet[0].LHasSystemAttributes.Count);
        }
    }
}
